﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Sudoku
{
    public partial class FormSudoku : Form
    {
        private Sudoku Sudoku;

        public FormSudoku() {
            InitializeComponent();

            //Centrage de la fenetre principale
            this.StartPosition = FormStartPosition.CenterScreen;

            //Initialisation de la datagrid
            initializeDatagridSudoku();
        }

        private void initializeDatagridSudoku()
        {
            //Taille hauteur des lignes pour la mise en forme
            this.dataGridSudoku.RowTemplate.Height = 50;
            this.dataGridSudoku.RowTemplate.DefaultCellStyle.Padding = new Padding(15);
            this.dataGridSudoku.DefaultCellStyle.Font = new Font(Control.DefaultFont, FontStyle.Bold);
            this.dataGridSudoku.DefaultCellStyle.BackColor = Color.Gainsboro;

            //On initialise les lignes de la datagridview (colonnes traitées par l'interface)
            string[] row1 = new string[] { "", "", "", "", "", "", "", "", "" };
            string[] row2 = new string[] { "", "", "", "", "", "", "", "", "" };
            string[] row3 = new string[] { "", "", "", "", "", "", "", "", "" };
            string[] row4 = new string[] { "", "", "", "", "", "", "", "", "" };
            string[] row5 = new string[] { "", "", "", "", "", "", "", "", "" };
            string[] row6 = new string[] { "", "", "", "", "", "", "", "", "" };
            string[] row7 = new string[] { "", "", "", "", "", "", "", "", "" };
            string[] row8 = new string[] { "", "", "", "", "", "", "", "", "" };
            string[] row9 = new string[] { "", "", "", "", "", "", "", "", "" };

            object[] rows = new object[] { row1, row2, row3, row4, row5, row6, row7, row8, row9 };

            foreach (string[] rowArray in rows)
            {
                this.dataGridSudoku.Rows.Add(rowArray);
            }

            this.dataGridSudoku.Columns[2].DividerWidth = 2;
            this.dataGridSudoku.Columns[5].DividerWidth = 2;
            this.dataGridSudoku.Rows[2].DividerHeight = 2;
            this.dataGridSudoku.Rows[5].DividerHeight = 2;

            this.dataGridSudoku.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridSudoku_EditingControlShowing);

        }//initializeDatagridSudoku()

        private void Generer()
        {
            //On vide la grille avant de commencer, un clear nécessite la réinitialisation des lignes de données de la grille
            this.dataGridSudoku.Rows.Clear();
            initializeDatagridSudoku();

            //Instanciation d'une nouvelle grille de sudoku
            Sudoku = new Sudoku();
            Sudoku.generer();

            //Nombre de chiffres à masquer
            int resteAMasquer = 20;

            for (int i = 0; i < Sudoku.getGrilleSudoku().GetLength(0); i++)
                for (int j = 0; j < Sudoku.getGrilleSudoku().GetLength(1); j++)
                {
                    if (resteAMasquer > 0 &&
                            (i % 4 == 0 && j % 4 == 0) ||
                            (i % 4 == 1 && j % 4 == 1) ||
                            (i % 4 == 2 && j % 4 == 3) ||
                            (i % 4 == 1 && j % 4 == 3))
                    {
                        //Pas d'affichage de la valeur dans la case
                        //Modifie la valeur de la cellule j de la ligne i
                        this.dataGridSudoku.Rows[i].Cells[j].Value = "";
                        this.dataGridSudoku.Rows[i].Cells[j].Style.ForeColor = Color.Red;
                    }
                    else
                    {
                        //Modifie la valeur de la cellule j de la ligne i
                        this.dataGridSudoku.Rows[i].Cells[j].Value = Sudoku.getGrilleSudoku()[i, j].ToString();
                        this.dataGridSudoku.Rows[i].Cells[j].ReadOnly = true;
                    }
                }
        }//Generer()

        private void dataGridSudoku_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);
        }

        private void Control_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
                Console.WriteLine("Element saisi : " + e.KeyChar);
            }
        }

        private void FormSudoku_Load(object sender, EventArgs e)
        {
            Generer();
        }

        //Génère la grille de sudoku et l'affiche dans la datagrid
        private void btnGenerer_Click(object sender, EventArgs e)
        {
            Generer();
        }


        private void btnVerifiver_Click(object sender, EventArgs e)
        {
            //Booleen de vérification
            Boolean isValid = true;

            //Parcours du tableau et vérification des cases
            try
            {
                for (int i = 0; i < Sudoku.getGrilleSudoku().GetLength(0); i++)
                    for (int j = 0; j < Sudoku.getGrilleSudoku().GetLength(1); j++)
                    {
                        if (this.dataGridSudoku.Rows[i].Cells[j].Value.ToString() != Sudoku.getGrilleSudoku()[i, j].ToString())
                            isValid = false;
                    }

                if (isValid)
                    MessageBox.Show("Vous avez remporté la partie !");
                else
                    MessageBox.Show("La grille n'est pas correcte, retentez votre chance !");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Vous devez générer votre grille au préalable");
                Console.WriteLine(ex.Message);
            }
        }
        
    }
}
